﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Customer_Profile.Admin
{
    public partial class UserInfoModify : System.Web.UI.Page
    {
        public int UserId = -1;
        public string sOldPaw = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null)
                UserId = Convert.ToInt32(Request.QueryString["ID"].ToString());
            else
            {
                KVB.Common.JavaScript.ViewMsgGoUrl("/login.aspx", "Please Login System");
            }
            if (UserId != -1)
            {

                DataSet ds = UserDao.getUserInfo(UserId);
                this.txtLoginID.Value = ds.Tables[0].Rows[0]["User_LoginID"].ToString();
                this.txtUserName.Value = ds.Tables[0].Rows[0]["User_Name"].ToString();
                sOldPaw = ds.Tables[0].Rows[0]["User_Password"].ToString();
            }
        }

        protected void ibtnModify_ServerClick(object sender, EventArgs e)
        {
            string str_old_password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.oldUserPass.Value.Trim(), "MD5").ToLower();
            if (str_old_password.Equals(sOldPaw))
            {
                if (Request.QueryString["ID"] != null)
                    UserId = Convert.ToInt32(Request.QueryString["ID"].ToString());
                else
                {
                    KVB.Common.JavaScript.ViewMsgGoUrl("/login.aspx", "未登录或登陆超时!请重新登陆。");
                }
                UserInfoEntity userCon = new UserInfoEntity();

                userCon.UserId = UserId.ToString();
                userCon.UserPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtUserPass.Value.Trim(), "MD5").ToLower(); //this.txtUserPass.Value;
                userCon.UserName = this.txtUserName.Value.Trim();
                try
                {
                    bool isSucess = UserDao.updateUserPassword(userCon);
                    if (isSucess == true)
                    {
                        Response.Write("<script language=javascript>alert('修改成功!');</script>");
                    }
                }
                catch
                {
                    Response.Write("<script language=javascript>alert('数据操作出现异常，没有修改成功!');</script>");
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('旧密码有误，请重新输入!');</script>");
            }
        }
    }
}
